﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//using hlib.Code;
using SalesSys.Comm.BLL;
using SalesSys.Comm.DAL;
using SalesSys.Comm.Utility;
using SalesSys.DAL;
using SalesSys.DAL.Models;
using SalesSys.DAL;

namespace SalesSys.BLL
{
    public class CustomerBalanceBLL : BaseBLL<CustomerBalance, Guid>
    {
        CustomerBLL customerBLL = BLL.BllFactory.GetBll<CustomerBLL>();
        /// <summary>
        /// 获取我的客户列表
        /// </summary>
        /// <param name="searchKey"></param>
        /// <param name="startTime"></param>
        /// <param name="endTime"></param>
        /// <param name="state"></param>
        /// <param name="qinfo"></param>
        /// <param name="isDel"></param>
        /// <returns></returns>
        public KeyValuePair<int, IEnumerable<Customer>> GetMyCustomers(string name, Guid EmployeeId, int page, int rows)
        {
            var query = Rep.Entities.Where(p => p.EmployeeId == EmployeeId).ToList();
            List<Customer> customerList = new List<Customer>();
            if (query.Count > 0)
            {
                foreach (var item in query)
                {
                    Customer customer = customerBLL.Details(item.CustomerId);
                    if (customer != null)
                    {
                        if (!string.IsNullOrWhiteSpace(name))
                        {
                            if ((customer.ShortName.Contains(name) || customer.Name.Contains(name)) && !customer.IsDelete&&customer.ApprovalState==ApprovalState.Agree)
                            {
                                if (!customerList.Contains(customer)) {
                                    customerList.Add(customer);
                                }
                            }
                        }
                        else
                        {
                            if (!customer.IsDelete && customer.ApprovalState == ApprovalState.Agree)
                            {
                                if (!customerList.Contains(customer))
                                {
                                    customerList.Add(customer);
                                }
                            }
                        }
                    }
                }
            }
            var count = customerList.Count;
            var list = customerList.OrderByDescending(p => p.CreateTime).Paging(page, rows);
            return new KeyValuePair<int, IEnumerable<Customer>>(count, list);
        }
        public KeyValuePair<int, IEnumerable<Customer>> GetMyAllCustomers(string name, Guid EmployeeId, int page, int rows)
        {
            var query = Rep.Entities.Where(p => p.EmployeeId == EmployeeId).ToList();
            List<Customer> customerList = new List<Customer>();
            if (query.Count > 0)
            {
                foreach (var item in query)
                {
                    Customer customer = customerBLL.Details(item.CustomerId);
                    if (customer != null)
                    {
                        if (!string.IsNullOrWhiteSpace(name))
                        {
                            if ((customer.ShortName.Contains(name) || customer.Name.Contains(name)) && !customer.IsDelete )
                            {
                                if (!customerList.Contains(customer))
                                {
                                    customerList.Add(customer);
                                }
                            }
                        }
                        else
                        {
                            if (!customer.IsDelete )
                            {
                                if (!customerList.Contains(customer))
                                {
                                    customerList.Add(customer);
                                }
                            }
                        }
                    }
                }
            }
            var count = customerList.Count;
            var list = customerList.OrderByDescending(p => p.CreateTime).Paging(page, rows);
            return new KeyValuePair<int, IEnumerable<Customer>>(count, list);
        }

        /// <summary>
        /// 通过 客户+客户经理+户头 查询当前余额
        /// </summary>
        /// <param name="customerId"></param>
        /// <param name="employeeId"></param>
        /// <param name="accountId"></param>
        /// <returns></returns>
        public decimal GetAccountAmount(Guid customerId, Guid employeeId, Guid accountId)
        {
            var query =
                Rep.Entities.FirstOrDefault(
                    p => p.AccountId == accountId && p.CustomerId == customerId && p.EmployeeId == employeeId);
            if (query != null)
            {
                return query.Balance;
            }
            return 0;
        }

        /// <summary>
        /// 通过 客户+客户经理+户头 判断当前余额是否存在
        /// </summary>
        /// <param name="customerId"></param>
        /// <param name="employeeId"></param>
        /// <param name="accountId"></param>
        /// <returns></returns>
        public bool IsExistAccountAmount(Guid customerId, Guid employeeId, Guid accountId)
        {
            var query =
              Rep.Entities.FirstOrDefault(
                  p => p.AccountId == accountId && p.CustomerId == customerId && p.EmployeeId == employeeId);
            if (query != null)
            {
                return true;
            }
            return false;
        }
     
        /// <summary>
        /// 判断该客户和该客户经理是否有关联
        /// </summary>
        /// <param name="customerId"></param>
        /// <param name="employeeId"></param>
        /// <returns></returns>
        public bool IsExistAccount(Guid customerId, Guid employeeId)
        {
            var query =
              Rep.Entities.FirstOrDefault(
                  p =>  p.CustomerId == customerId && p.EmployeeId == employeeId);
            if (query != null)
            {
                return true;
            }
            return false;
        }
        /// <summary>
        /// 通过 客户+客户经理+户头 判断当前余额信息
        /// </summary>
        /// <param name="customerId"></param>
        /// <param name="employeeId"></param>
        /// <param name="accountId"></param>
        /// <returns></returns>
        public CustomerBalance GetAccountAmountInfo(Guid customerId, Guid employeeId, Guid accountId)
        {
            var query =
              Rep.Entities.FirstOrDefault(
                  p => p.AccountId == accountId && p.CustomerId == customerId && p.EmployeeId == employeeId);
            if (query != null)
            {
                return query;
            }
            return new CustomerBalance();
        }

        /// <summary>
        /// 获取当前客户经理+客户 账户下所有余额之和
        /// </summary>
        /// <param name="customerId"></param>
        /// <param name="employeeId"></param>
        /// <returns></returns>
        public decimal GetAccountAllAmount(Guid customerId, Guid employeeId)
        {
            var query =
                Rep.Entities.Where(
                    p => p.CustomerId == customerId && p.EmployeeId == employeeId).Sum(p=>p.Balance);
            if (query != null)
            {
                return query;
            }
            return 0;
        }
    }
}
